/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.parameter.service.impl;

import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.core.time.TimeTool;
import com.wallellen.parameter.dao.DictionaryTypeDaoInter;
import com.wallellen.parameter.domain.AloneDictionaryType;
import com.wallellen.parameter.service.DictionaryEntityServiceInter;
import com.wallellen.parameter.service.DictionaryTypeServiceInter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Vector;

/**
 * Created by walle on 12/12/15.
 */

@Service
public class DictionaryTypeServiceImpl
        implements DictionaryTypeServiceInter {

    @Resource
    private DictionaryTypeDaoInter dictionarytypeDao;

    @Resource
    private DictionaryEntityServiceInter dicManager;
    private static final List<AloneDictionaryType> DictionaryTypeList = new Vector();

    public String getConfigValue(String key) {
        return null;
    }

    @Transactional
    public void deleteEntity(String entity, LoginUser user) {
        AloneDictionaryType dictypeEntity = this.dictionarytypeDao.getEntity(entity);
        this.dictionarytypeDao.deleteEntityByTreecode(dictypeEntity.getId());
        this.dicManager.editComments(dictypeEntity.getEntity());
        DictionaryEntityServiceImpl.dicCache.remove(this.dicManager.getDicKey(dictypeEntity.getEntity()));
    }

    @Transactional
    public AloneDictionaryType editEntity(AloneDictionaryType entity, LoginUser user) {
        AloneDictionaryType entity2 = getEntity(entity.getId());
        entity2.setName(entity.getName());
        entity2.setEntitytype(entity.getEntitytype());
        entity2.setSort(entity.getSort());
        entity2.setState(entity.getState());
        entity2.setComments(entity.getComments());
        entity2.setUtime(TimeTool.getTimeDate12());
        entity2.setMuser(user.getName());
        this.dictionarytypeDao.editEntity(entity2);

        if ((entity.getEntity() == null) || (entity.getEntity().equals(""))) {
            throw new RuntimeException("无法关联数据字典！");
        }
        this.dicManager.editComments(entity.getEntity());
        DictionaryEntityServiceImpl.dicCache.remove(this.dicManager.getDicKey(entity.getEntity()));
        return entity2;
    }

    public List<AloneDictionaryType> getAllEntity() {
        return null;
    }

    public int getAllListNum() {
        return 0;
    }

    @Transactional
    public AloneDictionaryType getEntity(String id) {
        if (id == null) {
            return null;
        }
        return this.dictionarytypeDao.getEntity(id);
    }

    @Transactional
    public AloneDictionaryType insertEntity(AloneDictionaryType entity, LoginUser user) {
        if ((entity.getParentid() == null) ||
                (entity.getParentid().trim().length() <= 0)) {
            entity.setParentid("NONE");
        }
        entity.setCtime(TimeTool.getTimeDate12());
        entity.setCuser(user.getId());
        entity.setUtime(TimeTool.getTimeDate12());
        entity.setMuser(user.getId());
        entity.setState("1");
        this.dictionarytypeDao.insertEntity(entity);

        AloneDictionaryType fatherEntity = getEntity(entity.getParentid());
        if (fatherEntity == null)
            entity.setTreecode(entity.getId());
        else {
            entity.setTreecode(fatherEntity.getTreecode() + entity.getId());
        }
        this.dictionarytypeDao.editEntity(entity);

        if ((entity.getEntity() == null) || (entity.getEntity().equals(""))) {
            throw new RuntimeException("无法关联数据字典！");
        }
        this.dicManager.editComments(entity.getEntity());
        DictionaryEntityServiceImpl.dicCache.remove(this.dicManager.getDicKey(entity.getEntity()));
        return entity;
    }

    public static List<AloneDictionaryType> getdictionarytypeList() {
        return DictionaryTypeList;
    }

    public DictionaryTypeDaoInter getdictionarytypeDao() {
        return this.dictionarytypeDao;
    }

    public void setdictionarytypeDao(DictionaryTypeDaoInter dictionarytypeDao) {
        this.dictionarytypeDao = dictionarytypeDao;
    }

    public DictionaryEntityServiceInter getDicManager() {
        return this.dicManager;
    }

    public void setDicManager(DictionaryEntityServiceInter dicManager) {
        this.dicManager = dicManager;
    }
}